<?php

namespace Database\Seeders;

use App\Models\LearnWord\LearnWord;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class LearnWordDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $learn_words = DB::connection('rjmysql')->table('learn_words')->get()->toArray();
        $learn_words = json_decode(json_encode($learn_words), true);

        foreach ($learn_words as $learn_word) {
            if (DB::table('categorys')->where('id', $learn_word['grade_id'])->exists() &&
                DB::table('click_learns')->where('id', $learn_word['click_learn_id'])->exists()) {
                LearnWord::query()->updateOrCreate(
                    ['id' => $learn_word['id']],
                    $learn_word
                );
            }
        }

        $this->command->info('背单词数据填充完成！');
    }
}
